// ButtonManager.cpp
#include "buttonmanager.h"
#include <QVBoxLayout>

ButtonManager::ButtonManager(QGridLayout *layout, QObject *parent) : QObject(parent) {
    buttonWidget = new QWidget(); // 不设置父部件

    // 创建6个按钮
    QStringList buttonNames = {"一键代理", "一键建图", "一键存图", "一键导航和避障","一键跟随", "关闭建图", "关闭导航和避障", "重启ROS2"};
    // 将按钮添加到网格布局中
    for (int i = 0; i < buttonNames.size(); ++i) {
        QPushButton *button = createButton(buttonNames[i]);
        int row = i / 6; // 每行6个按钮
        int col = i % 6;
        layout->addWidget(button, row, col); // 将按钮添加到网格布局的指定位置
        // 连接按钮点击信号到槽函数,信号发送的是按钮名字对应的编号，即i
        connect(button, &QPushButton::clicked, this, [this, name = i]() {
            emit buttonClicked(name); // 发送按钮编号
        });
    }
}

QPushButton *ButtonManager::createButton(const QString &text) {
    QPushButton *button = new QPushButton(text);
    button->setFixedSize(200, 40); // 设置按钮大小
    return button;
}

QWidget *ButtonManager::getButtonWidget() const {
    return buttonWidget; // 返回按钮容器
}
